﻿using System;
using System.Collections.Generic;
using System.Linq;
using Helpers;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

namespace TaleWorlds.CampaignSystem.Election
{
	// Token: 0x02000271 RID: 625
	public class SettlementClaimantPreliminaryDecision : KingdomDecision
	{
		// Token: 0x06002107 RID: 8455 RVA: 0x0008CC3F File Offset: 0x0008AE3F
		internal static void AutoGeneratedStaticCollectObjectsSettlementClaimantPreliminaryDecision(object o, List<object> collectedObjects)
		{
			((SettlementClaimantPreliminaryDecision)o).AutoGeneratedInstanceCollectObjects(collectedObjects);
		}

		// Token: 0x06002108 RID: 8456 RVA: 0x0008CC4D File Offset: 0x0008AE4D
		protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
		{
			base.AutoGeneratedInstanceCollectObjects(collectedObjects);
			collectedObjects.Add(this.Settlement);
			collectedObjects.Add(this._ownerClan);
		}

		// Token: 0x06002109 RID: 8457 RVA: 0x0008CC6E File Offset: 0x0008AE6E
		internal static object AutoGeneratedGetMemberValueSettlement(object o)
		{
			return ((SettlementClaimantPreliminaryDecision)o).Settlement;
		}

		// Token: 0x0600210A RID: 8458 RVA: 0x0008CC7B File Offset: 0x0008AE7B
		internal static object AutoGeneratedGetMemberValue_ownerClan(object o)
		{
			return ((SettlementClaimantPreliminaryDecision)o)._ownerClan;
		}

		// Token: 0x0600210B RID: 8459 RVA: 0x0008CC88 File Offset: 0x0008AE88
		public SettlementClaimantPreliminaryDecision(Clan proposerClan, Settlement settlement) : base(proposerClan)
		{
			this.Settlement = settlement;
			this._ownerClan = settlement.OwnerClan;
		}

		// Token: 0x0600210C RID: 8460 RVA: 0x0008CCA4 File Offset: 0x0008AEA4
		public override bool IsAllowed()
		{
			return Campaign.Current.Models.KingdomDecisionPermissionModel.IsAnnexationDecisionAllowed(this.Settlement);
		}

		// Token: 0x0600210D RID: 8461 RVA: 0x0008CCC0 File Offset: 0x0008AEC0
		public override int GetProposalInfluenceCost()
		{
			return Campaign.Current.Models.DiplomacyModel.GetInfluenceCostOfAnnexation(base.ProposerClan);
		}

		// Token: 0x0600210E RID: 8462 RVA: 0x0008CCDC File Offset: 0x0008AEDC
		public override TextObject GetGeneralTitle()
		{
			TextObject textObject = new TextObject("{=XFr4IfXf} Change Owner of {SETTLEMENT}", null);
			textObject.SetTextVariable("SETTLEMENT", this.Settlement.Name);
			return textObject;
		}

		// Token: 0x0600210F RID: 8463 RVA: 0x0008CD00 File Offset: 0x0008AF00
		public override TextObject GetSupportTitle()
		{
			TextObject textObject = new TextObject("{=a48xlNUb}Should the owner of {SETTLEMENT} change?", null);
			textObject.SetTextVariable("SETTLEMENT", this.Settlement.Name);
			return textObject;
		}

		// Token: 0x06002110 RID: 8464 RVA: 0x0008CD24 File Offset: 0x0008AF24
		public override TextObject GetChooseTitle()
		{
			TextObject textObject = new TextObject("{=a48xlNUb}Should the owner of {SETTLEMENT} change?", null);
			textObject.SetTextVariable("SETTLEMENT", this.Settlement.Name);
			return textObject;
		}

		// Token: 0x06002111 RID: 8465 RVA: 0x0008CD48 File Offset: 0x0008AF48
		public override TextObject GetSupportDescription()
		{
			TextObject textObject = new TextObject("{=hBJbnoDn}{FACTION_LEADER} will decide if the owner of {SETTLEMENT} will change.", null);
			textObject.SetTextVariable("FACTION_LEADER", this.DetermineChooser().Leader.Name);
			textObject.SetTextVariable("SETTLEMENT", this.Settlement.Name);
			return textObject;
		}

		// Token: 0x06002112 RID: 8466 RVA: 0x0008CD88 File Offset: 0x0008AF88
		public override TextObject GetChooseDescription()
		{
			TextObject textObject = new TextObject("{=JHR4ySCf}As {?IS_FEMALE}queen{?}king{\\?} you must decide if the owner of {SETTLEMENT} should change.", null);
			textObject.SetTextVariable("IS_FEMALE", this.DetermineChooser().Leader.IsFemale ? 1 : 0);
			textObject.SetTextVariable("SETTLEMENT", this.Settlement.Name);
			return textObject;
		}

		// Token: 0x06002113 RID: 8467 RVA: 0x0008CDDC File Offset: 0x0008AFDC
		public override float CalculateMeritOfOutcome(DecisionOutcome candidateOutcome)
		{
			float num = 0f;
			float num2 = 50f;
			List<Clan> list = new List<Clan>();
			if (this._ownerClan.Kingdom != null)
			{
				list.AddRange(this._ownerClan.Kingdom.Clans.ToList<Clan>());
			}
			else
			{
				list.Add(this._ownerClan);
			}
			foreach (Clan clan in list)
			{
				int relation = clan.Leader.GetRelation(this._ownerClan.Leader);
				bool shouldSettlementOwnerChange = ((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome)candidateOutcome).ShouldSettlementOwnerChange;
				if ((!shouldSettlementOwnerChange && relation > 0) || (shouldSettlementOwnerChange && relation <= 0))
				{
					num += num2 * (float)MathF.Abs(relation);
				}
			}
			return num;
		}

		// Token: 0x06002114 RID: 8468 RVA: 0x0008CEB0 File Offset: 0x0008B0B0
		public override IEnumerable<DecisionOutcome> DetermineInitialCandidates()
		{
			yield return new SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome(true);
			yield return new SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome(false);
			yield break;
		}

		// Token: 0x06002115 RID: 8469 RVA: 0x0008CEB9 File Offset: 0x0008B0B9
		public override Clan DetermineChooser()
		{
			return ((Kingdom)this.Settlement.MapFaction).RulingClan;
		}

		// Token: 0x06002116 RID: 8470 RVA: 0x0008CED0 File Offset: 0x0008B0D0
		protected override bool ShouldBeCancelledInternal()
		{
			return this.Settlement.MapFaction != base.Kingdom;
		}

		// Token: 0x06002117 RID: 8471 RVA: 0x0008CEE8 File Offset: 0x0008B0E8
		public float CalculateSupport(Clan clan)
		{
			return this.DetermineSupport(clan, new SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome(true));
		}

		// Token: 0x06002118 RID: 8472 RVA: 0x0008CEF8 File Offset: 0x0008B0F8
		public override float DetermineSupport(Clan clan, DecisionOutcome possibleOutcome)
		{
			bool shouldSettlementOwnerChange = ((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome)possibleOutcome).ShouldSettlementOwnerChange;
			int num = clan.GetRelationWithClan(this._ownerClan);
			if (clan == this._ownerClan)
			{
				num = 300;
			}
			float num2 = 20f;
			if (this.Settlement.OwnerClan == clan)
			{
				num2 *= 20f;
			}
			else
			{
				num2 += (float)num * 0.7f;
				float totalStrength = this.Settlement.OwnerClan.TotalStrength;
				num2 += totalStrength * 0.01f;
			}
			float result = 0f;
			if (shouldSettlementOwnerChange)
			{
				result = -num2;
			}
			else if (!shouldSettlementOwnerChange)
			{
				result = num2;
			}
			return result;
		}

		// Token: 0x06002119 RID: 8473 RVA: 0x0008CF88 File Offset: 0x0008B188
		public override void DetermineSponsors(MBReadOnlyList<DecisionOutcome> possibleOutcomes)
		{
			foreach (DecisionOutcome decisionOutcome in possibleOutcomes)
			{
				if (((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome)decisionOutcome).ShouldSettlementOwnerChange)
				{
					decisionOutcome.SetSponsor(base.ProposerClan);
				}
				else
				{
					base.AssignDefaultSponsor(decisionOutcome);
				}
			}
		}

		// Token: 0x0600211A RID: 8474 RVA: 0x0008CFF4 File Offset: 0x0008B1F4
		protected override int GetInfluenceCostOfSupportInternal(Supporter.SupportWeights supportWeight)
		{
			if (this.Settlement.IsTown)
			{
				if (supportWeight == Supporter.SupportWeights.SlightlyFavor)
				{
					return 20;
				}
				if (supportWeight == Supporter.SupportWeights.StronglyFavor)
				{
					return 60;
				}
				if (supportWeight != Supporter.SupportWeights.FullyPush)
				{
					return 0;
				}
				return 200;
			}
			else
			{
				if (supportWeight == Supporter.SupportWeights.SlightlyFavor)
				{
					return 15;
				}
				if (supportWeight == Supporter.SupportWeights.StronglyFavor)
				{
					return 50;
				}
				if (supportWeight != Supporter.SupportWeights.FullyPush)
				{
					return 0;
				}
				return 150;
			}
		}

		// Token: 0x0600211B RID: 8475 RVA: 0x0008D044 File Offset: 0x0008B244
		public override void ApplyChosenOutcome(DecisionOutcome chosenOutcome)
		{
			if (((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome)chosenOutcome).ShouldSettlementOwnerChange)
			{
				SettlementClaimantDecision settlementClaimantDecision = new SettlementClaimantDecision(base.ProposerClan, this.Settlement, null, this._ownerClan);
				settlementClaimantDecision.IsEnforced = true;
				base.ProposerClan.Kingdom.AddDecision(settlementClaimantDecision, true);
			}
		}

		// Token: 0x0600211C RID: 8476 RVA: 0x0008D090 File Offset: 0x0008B290
		public override TextObject GetSecondaryEffects()
		{
			return new TextObject("{=CsZfuPOe}Voting against or in favor of the current owner of the settlement will affect your relation with that clan accordingly.", null);
		}

		// Token: 0x0600211D RID: 8477 RVA: 0x0008D0A0 File Offset: 0x0008B2A0
		public override void ApplySecondaryEffects(MBReadOnlyList<DecisionOutcome> possibleOutcomes, DecisionOutcome chosenOutcome)
		{
			foreach (DecisionOutcome decisionOutcome in possibleOutcomes)
			{
				bool shouldSettlementOwnerChange = ((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome)decisionOutcome).ShouldSettlementOwnerChange;
				foreach (Supporter supporter in decisionOutcome.SupporterList)
				{
					if (supporter.Clan.Leader != this._ownerClan.Leader)
					{
						ChangeRelationAction.ApplyRelationChangeBetweenHeroes(supporter.Clan.Leader, this._ownerClan.Leader, Campaign.Current.Models.DiplomacyModel.GetRelationChangeAfterVotingInSettlementOwnerPreliminaryDecision(supporter.Clan.Leader, shouldSettlementOwnerChange), true);
					}
				}
			}
		}

		// Token: 0x0600211E RID: 8478 RVA: 0x0008D188 File Offset: 0x0008B388
		public override TextObject GetChosenOutcomeText(DecisionOutcome chosenOutcome, KingdomDecision.SupportStatus supportStatus, bool isShortVersion = false)
		{
			TextObject textObject = TextObject.Empty;
			if (((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome)chosenOutcome).ShouldSettlementOwnerChange)
			{
				if (supportStatus == KingdomDecision.SupportStatus.Majority)
				{
					textObject = new TextObject("{=Zo65bOpH}{RULER.NAME} has decided to give {SETTLEMENT} to a new clan with the support of with {?RULER.GENDER}her{?}his{\\?} council.", null);
				}
				else if (supportStatus == KingdomDecision.SupportStatus.Minority)
				{
					textObject = new TextObject("{=w3sfcpoa}{RULER.NAME} has decided to give {SETTLEMENT} to a new clan despite the opposition of {?RULER.GENDER}her{?}his{\\?} council", null);
				}
				else
				{
					textObject = new TextObject("{=JzALLCEX}{RULER.NAME} has decided to give {SETTLEMENT} to a new clan, with {?RULER.GENDER}her{?}his{\\?} council evenly split on the matter.", null);
				}
			}
			else if (supportStatus == KingdomDecision.SupportStatus.Majority)
			{
				textObject = new TextObject("{=vkeHpUEB}{RULER.NAME} has decided against giving {SETTLEMENT} to a new clan with the support of with {?RULER.GENDER}her{?}his{\\?} council.", null);
			}
			else if (supportStatus == KingdomDecision.SupportStatus.Minority)
			{
				textObject = new TextObject("{=9Cbeagow}{RULER.NAME} has decided against giving {SETTLEMENT} to a new clan over the objections of {?RULER.GENDER}her{?}his{\\?} council.", null);
			}
			else
			{
				textObject = new TextObject("{=fP8NHthR}{RULER.NAME} has decided against giving {SETTLEMENT} to a new clan, with {?RULER.GENDER}her{?}his{\\?} council evenly split on the matter.", null);
			}
			textObject.SetTextVariable("SETTLEMENT", this.Settlement.Name);
			textObject.SetTextVariable("KINGDOM", this.Settlement.MapFaction.InformalName);
			StringHelpers.SetCharacterProperties("RULER", this.Settlement.MapFaction.Leader.CharacterObject, textObject, false);
			return textObject;
		}

		// Token: 0x0600211F RID: 8479 RVA: 0x0008D260 File Offset: 0x0008B460
		public override DecisionOutcome GetQueriedDecisionOutcome(MBReadOnlyList<DecisionOutcome> possibleOutcomes)
		{
			return possibleOutcomes.FirstOrDefault((DecisionOutcome t) => ((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome)t).ShouldSettlementOwnerChange);
		}

		// Token: 0x06002120 RID: 8480 RVA: 0x0008D287 File Offset: 0x0008B487
		public override KingdomDecision GetFollowUpDecision()
		{
			return base.Kingdom.UnresolvedDecisions.FirstOrDefault(delegate(KingdomDecision x)
			{
				SettlementClaimantDecision settlementClaimantDecision;
				return (settlementClaimantDecision = (x as SettlementClaimantDecision)) != null && settlementClaimantDecision.Settlement == this.Settlement;
			});
		}

		// Token: 0x04000A51 RID: 2641
		[SaveableField(400)]
		public readonly Settlement Settlement;

		// Token: 0x04000A52 RID: 2642
		[SaveableField(401)]
		private Clan _ownerClan;

		// Token: 0x02000582 RID: 1410
		public class SettlementClaimantPreliminaryOutcome : DecisionOutcome
		{
			// Token: 0x060045C7 RID: 17863 RVA: 0x0014AB39 File Offset: 0x00148D39
			internal static void AutoGeneratedStaticCollectObjectsSettlementClaimantPreliminaryOutcome(object o, List<object> collectedObjects)
			{
				((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome)o).AutoGeneratedInstanceCollectObjects(collectedObjects);
			}

			// Token: 0x060045C8 RID: 17864 RVA: 0x0014AB47 File Offset: 0x00148D47
			protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
			{
				base.AutoGeneratedInstanceCollectObjects(collectedObjects);
			}

			// Token: 0x060045C9 RID: 17865 RVA: 0x0014AB50 File Offset: 0x00148D50
			internal static object AutoGeneratedGetMemberValueShouldSettlementOwnerChange(object o)
			{
				return ((SettlementClaimantPreliminaryDecision.SettlementClaimantPreliminaryOutcome)o).ShouldSettlementOwnerChange;
			}

			// Token: 0x060045CA RID: 17866 RVA: 0x0014AB62 File Offset: 0x00148D62
			public override TextObject GetDecisionTitle()
			{
				TextObject textObject = new TextObject("{=kakxnaN5}{?SUPPORT}Yes{?}No{\\?}", null);
				textObject.SetTextVariable("SUPPORT", this.ShouldSettlementOwnerChange ? 1 : 0);
				return textObject;
			}

			// Token: 0x060045CB RID: 17867 RVA: 0x0014AB87 File Offset: 0x00148D87
			public override TextObject GetDecisionDescription()
			{
				if (this.ShouldSettlementOwnerChange)
				{
					return new TextObject("{=1bbsq6uw}We should award this fief to a new clan", null);
				}
				return new TextObject("{=uBcEUdxu}Ownership of the fief should remain as it is", null);
			}

			// Token: 0x060045CC RID: 17868 RVA: 0x0014ABA8 File Offset: 0x00148DA8
			public override string GetDecisionLink()
			{
				return null;
			}

			// Token: 0x060045CD RID: 17869 RVA: 0x0014ABAB File Offset: 0x00148DAB
			public override ImageIdentifier GetDecisionImageIdentifier()
			{
				return null;
			}

			// Token: 0x060045CE RID: 17870 RVA: 0x0014ABAE File Offset: 0x00148DAE
			public SettlementClaimantPreliminaryOutcome(bool shouldSettlementOwnerChange)
			{
				this.ShouldSettlementOwnerChange = shouldSettlementOwnerChange;
			}

			// Token: 0x0400170A RID: 5898
			[SaveableField(400)]
			public bool ShouldSettlementOwnerChange;
		}
	}
}
